Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tab completions #209

Merged
merged 12 commits into from
Aug 17, 2022
Merged

Support tab completions #209

merged 12 commits into from
Aug 17, 2022

Conversation

jotaen
Copy link
Owner

@jotaen jotaen commented Aug 17, 2022

Resolves #177.

Thanks to @chairmank for the groundwork and for figuring out all the wiring.

Some notes:

  • As discussed in the ticket, the willabides/kongplete library does not support default completions, e.g. for the file path. Therefore, I had to create a fork in order to fix this. I’m also actually not sure, whether the library is still actively maintained.
  • I’ve abandoned the --this-*** placeholder flags, that I originally introduced to keep the help output more concise. But I couldn’t figure out a way to make this work with the completion predictors. These wildcard flags also create other complications, e.g. if you mistype something like klog total --this-yaer, then kong (the CLI library) is unable to hint the correct flags, since they are hidden. So all in all, it seems like just including all flags in the help output is the superior solution.
  • I’ve tested with all support shells (zsh, bash, fish)

chairmank and others added 12 commits April 14, 2022 22:25
This is an initial effort to address
#177

With this change, klog uses github.com/posener/complete and
github.com/willabides/kongplete to generate completions for klog's own
commands.

There are two coordinated changes that make this feature work:

1. When the `COMP_LINE` environment variable is set, `klog` runs in a
   special command completion mode. It parses the value of `COMP_LINE`
   (the current command line), prints to stdout a list of possible
   completions based on what is on the command line so far, and exits
   early.
2. `klog completion` prints to stdout a code snippet that defines a
   completion specification in the current shell. When this code snippet
   is executed in a shell session, the shell will thereafter invoke the
   same `klog` executable (with the `COMP_LINE` environment variable
   set) to generate possible completions.

For example, in bash, completions can be enabled by running

    source <(log completion)

Once bash completions are enabled, pressing tab after typing "klog "

    klog [Tab]

will present the available klog commands.

In the current implementation, the generated completions are rudimentary
and lack ergonomics.

There is zero test coverage for this new feature. Because it relies on
interaction with the shell, this feature is not easy to test.
@jotaen jotaen mentioned this pull request Aug 17, 2022
@jotaen jotaen merged commit 46141fa into main Aug 17, 2022
@jotaen jotaen deleted the shell-completion branch August 17, 2022 10:10
@jotaen jotaen mentioned this pull request Aug 17, 2022
@jotaen jotaen changed the title Shell completions Support tab completions Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell completion
2 participants